package sign_in.loginsystem.aop;


import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sign_in.loginsystem.annotation.Permissions;
import sign_in.loginsystem.model.util.Result;
import sign_in.loginsystem.tool.Analyze;

import java.lang.reflect.Method;

@Aspect
@Component
public class PemAndLogAOP {

    @Autowired
    Analyze analyze;

    @Autowired
    HttpServletRequest request;

    @Pointcut("@annotation(sign_in.loginsystem.annotation.Permissions)")
    private void pt(){}

    @Around("pt()")
    public Object permissionOrNot(ProceedingJoinPoint joinPoint) throws Throwable {
        String token = request.getHeader("Token");
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        Permissions param = method.getAnnotation(Permissions.class);
        if (!analyze.permissionOrNot(param.permission(), token)) {
            System.out.println("没有权限");
            return Result.noPem();
        }
        System.out.println("权限认证成功");
        return joinPoint.proceed();
    }
}
